import request from '../../../api/request'
const NEWS = {
  namespaced: true,
  state: {
    leftMenu: [],
    rightFe: [],
    rightNew: [],
    breadcrumb: {
      leaveOne: '',
      leaveTwo: '',
      leaveThree: ''
    },
    currTime: ''  // 获取时间戳
  },
  mutations: {
    getCurrTime (state, data) {   // 获取时间戳
      state.currTime = data
    },
    initMenu (state, data) {
      state.leftMenu = data
    },
    modifyStatus (state, data) {
      state.breadcrumb.leaveOne = ''
      state.breadcrumb.leaveTwo = ''
      state.breadcrumb.leaveThree = ''
      state.leftMenu.forEach((item, index) => {
        if (index === data.index) {
          item.status = true
          item.height = item.list.length * 32
          state.breadcrumb.leaveOne = item.name
          item.list.forEach((items) => {
            if (items.id === data.id) {
              items.status = true
              state.breadcrumb.leaveTwo = items.name
            } else {
              items.status = false
            }
          })
        } else {
          item.status = false
          item.height = 0
        }
      })
    },
    saveRightFe (state, data) {
      let arr = []
      data.forEach((item, index) => {
        index ? (item.down = false) : (item.down = true)
        arr.push(item)
      })
      state.rightFe = arr
    },
    updataFe (state, data) {
      state.rightFe.forEach((item, index) => {
        (data === index) ? (item.down = true) : (item.down = false)
      })
    },
    saveRightNew (state, data) {
      state.rightNew = data
    },
    saveLeaveOne (state, name) {
      state.breadcrumb.leaveOne = name
    },
    saveLeaveTwo (state, name) {
      state.breadcrumb.leaveTwo = name
    },
    saveLeaveThree (state, name) {
      state.breadcrumb.leaveThree = name
    }
  },
  actions: {
    gettime (context) {   // 获取时间戳
      var d = new Date()
      var n = d.getTime()
      context.commit('getCurrTime', n)
    },
    getMenue (context, data) {
      request.get('getMenu').then((response) => {
        let arr = []
        if (response.data.code === '200' || response.data.code === '401') {
          response.data.data.menu.forEach((item, index) => {
            if (index === data.index) {
              item.status = true
              item.height = item.list.length * 32
              context.commit('saveLeaveOne', item.name)
            } else {
              item.status = false
              item.height = 0
            }
            item.list.forEach((items) => {
              if (items.id === data.id && data.index === index) {
                items.status = true
                context.commit('saveLeaveTwo', items.name)
              } else {
                items.status = false
              }
            })
            console.log(item)
            arr.push(item)
          })
          context.commit('initMenu', arr)
        }
      })
    },
    getRightContent (context, data) {
      request.get('newsRightContent', data).then((response) => {
        if (response.data.code === '200') {
          if (data.category_id === 1) {
            context.commit('saveRightFe', response.data.data.items)
          } else if (data.category_id === 2) {
            context.commit('saveRightNew', response.data.data.items)
          }
        }
      })
    }
  }
}
export default NEWS
